package com.frcdi.qms.common.utils;


import org.apache.commons.lang3.Validate;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Fgn
 * @version 1.0
 * @date 2022/1/6 10:27
 */
public class JudgeAttributesIsEqualedUtil<T> {
    public static <T> String contrastObj(Object oldBean, Object newBean) {
        // 创建字符串拼接对象
        StringBuilder str = new StringBuilder();
        // 转换为传入的泛型T
        T pojo1 = (T) oldBean;
        T pojo2 = (T) newBean;
        // 通过反射获取类的Class对象
        Class clazz = pojo1.getClass();
        Class clazz1 = pojo2.getClass();
        // 获取类型及字段属性
        List fieldList = getAllFieldsList(clazz);
        Field[] fields = (Field[]) fieldList.stream().toArray(Field[]::new);
        return ToGetAttributeNameUtil.toGetAttributeName(fields, pojo1, pojo2, str,clazz,clazz1);
    }


    public static List<Field> getAllFieldsList(final Class cls) {

        Validate.isTrue(cls != null, "The class must not be null");

        final List<Field> allFields = new ArrayList();

        Class currentClass = cls;

        while (currentClass != null) {

            final Field[] declaredFields = currentClass.getDeclaredFields();

            for (final Field field : declaredFields) {

                allFields.add(field);

            }

            currentClass = currentClass.getSuperclass();

        }

        return allFields;

    }

    public static <T> String ObjToString(Object bean) {
        // 创建字符串拼接对象
        StringBuilder str = new StringBuilder();
        // 转换为传入的泛型T
        T pojo1 = (T) bean;
        // 通过反射获取类的Class对象
        Class clazz = pojo1.getClass();
        List fieldList = getAllFieldsList(clazz);
        Field[] fields = (Field[]) fieldList.stream().toArray(Field[]::new);
        return ToGetAttributeNameUtil.toGetAttributeName(fields, pojo1,  str,clazz);
    }
}
